import {reqGoodInfo,reqAddOrderUpdateShopCart} from "../api";
import {getUUID} from '../utils/uuid_token'
const state={
  goodInfo:{},
  uuid_token:getUUID()
}
const getters={
  categoryView(state) {
    return state.goodInfo.categoryView||{}//为了防止报错必须在这里加个空对象(数据还没获取到时防止undified而报错,加个空对象)
  },
  skuInfo(state){
    return state.goodInfo.skuInfo||{}
  },
  spuSaleAttrList(state){
    return state.goodInfo.spuSaleAttrList||[]
  }
}
const mutations={
  GETGOODINFO(state,goodInfo){
    state.goodInfo=goodInfo.data
    console.log(state.goodInfo)
  }
}
const actions={
  async getGoodInfo({commit},skuId){
       let result= await reqGoodInfo(skuId)
    commit('GETGOODINFO',result.data)
  },
  async addOrderUpdateShopCart({commit},{skuId,skuNum}){
    let result=await reqAddOrderUpdateShopCart(skuId,skuNum)
    console.log(result)
    if(result.data.code==200){
      return 'ok'//成功
    }else{
      return Promise.reject(new Error('faile'))//失败
    }
  }
}
export default {
  state,
  getters,
  mutations,
  actions
}
